﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System.Diagnostics;
using wpf.prism.Models;
using wpf.prism.Serivces;

namespace wpf.prism.ViewModels
{
    public class MainViewModel : BindableBase, INavigationAware
    {
        public INavigationMenuService NavigationService { get; }
        public MainViewModel(INavigationMenuService navigationMenuService, IRegionManager regionManager)
        {
            NavigationService = navigationMenuService;
            this.regionManager = regionManager;
        }


        private bool isTopDrawerOpen;
        private readonly IRegionManager regionManager;

        public bool IsTopDrawerOpen
        {
            get { return isTopDrawerOpen; }
            set { isTopDrawerOpen = value; RaisePropertyChanged(); }
        }


        #region Commands
        public DelegateCommand<NavigationItem> NavigateCommand => new DelegateCommand<NavigationItem>(OnNavigation);

        #endregion


        private void OnNavigation(NavigationItem item)
        {
            if (item is null) return;

            if (item.Key.Equals("All"))
            {
                IsTopDrawerOpen = true;
            }
        }

        /// <summary>
        /// 导航视图 
        /// </summary>
        /// <param name="viewName">视图名称</param>
        private void NavigateView(string viewName)
        {
            regionManager.Regions["MainViewRegion"].RequestNavigate(viewName, back =>
            {
                if (!(bool)back.Result)
                {
                    Debug.WriteLine($"导航到 {viewName} 视图失败！Message: {back.Error.Message}");
                }
            });
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            NavigationService.InitMenus();
            NavigateView("DashboardView");
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
    }
}
